import request from '@/utils/request'

function toFormUrlEncoded(data) {
	const params = new URLSearchParams()
	Object.entries(data).forEach(([key, value]) => {
		if (value !== undefined && value !== null) {
			params.append(key, value)
		}
	})
	return params
}

export function uploadResume(userId, file) {
	const formData = new FormData()
	formData.append('userId', userId)
	formData.append('file', file)
	return request.post('/resume/upload', formData, {
		headers: { 'Content-Type': 'multipart/form-data' }
	})
}

export function getTaskStatus(taskId) {
	return request.get(`/resume/task-status/${taskId}`)
}

export function getParseResult(resumeId) {
	return request.get(`/resume/parse-result/${resumeId}`)
}

export function startEvaluation(userId, resumeId, targetPositionId) {
	return request.post('/resume/evaluation', toFormUrlEncoded({ userId, resumeId, targetPositionId }), {
		headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
	})
}

export function getEvaluationReportById(reportId) {
	return request.get(`/resume/evaluation/${reportId}`)
}

export function startOptimization(userId, resumeId, targetPositionId) {
	return request.post('/resume/optimization', toFormUrlEncoded({ userId, resumeId, targetPositionId }), {
		headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
	})
}

export function getOptimizationById(optimizationId) {
	return request.get(`/resume/optimization/${optimizationId}`)
}

// RAG 问答已下线

export function getLatestAnalysisByUser(userId) {
	return request.get(`/resume/latest-by-user/${userId}`)
}

export function getSimilarityReport(resumeId, topK = 5, threshold = 0.9) {
	return request.get(`/resume/similarity/${resumeId}`, { params: { topK, threshold } })
}